package com.ruoyi.web.controller.weixin;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.weixin.domain.GoodsSpu;
import com.ruoyi.weixin.service.IGoodsSpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商品Controller
 *
 * @author dqianhe
 * @date 2022-04-27
 */
@RestController
@RequestMapping("/weixin/spu")
public class GoodsSpuController extends BaseController {
    @Autowired
    private IGoodsSpuService goodsSpuService;

    /**
     * 查询商品列表
     */
    @PreAuthorize("@ss.hasPermi('weixin:spu:list')")
    @GetMapping("/list")
    public TableDataInfo list(GoodsSpu goodsSpu) {
        startPage();
        List<GoodsSpu> list = goodsSpuService.selectGoodsSpuList(goodsSpu);
        return getDataTable(list);
    }

    /**
     * 导出商品列表
     */
    @PreAuthorize("@ss.hasPermi('weixin:spu:export')")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GoodsSpu goodsSpu) {
        List<GoodsSpu> list = goodsSpuService.selectGoodsSpuList(goodsSpu);
        ExcelUtil<GoodsSpu> util = new ExcelUtil<GoodsSpu>(GoodsSpu.class);
        util.exportExcel(response, list, "商品数据");
    }

    /**
     * 获取商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('weixin:spu:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(goodsSpuService.selectGoodsSpuById(id));
    }

    /**
     * 新增商品
     */
    @PreAuthorize("@ss.hasPermi('weixin:spu:add')")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GoodsSpu goodsSpu) {
        return toAjax(goodsSpuService.insertGoodsSpu(goodsSpu));
    }

    /**
     * 修改商品
     */
    @PreAuthorize("@ss.hasPermi('weixin:spu:edit')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GoodsSpu goodsSpu) {
        return toAjax(goodsSpuService.updateGoodsSpu(goodsSpu));
    }

    /**
     * 删除商品
     */
    @PreAuthorize("@ss.hasPermi('weixin:spu:remove')")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(goodsSpuService.deleteGoodsSpuByIds(ids));
    }
}
